package com.mfs.common.utills;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
import java.util.concurrent.*;

/**
 * 邮件发送工具类
 * @author mfs
 * @date 2022/05/04
 * */
public class MailUtil {
    private static final ExecutorService emailThreadPool = new ThreadPoolExecutor(4, 64, 30,
            TimeUnit.MINUTES, new LinkedBlockingDeque<Runnable>(), new ThreadFactory() {
        private long id = 0;
        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r);
            thread.setName("EmailSendThread-" + id ++);
            thread.setDaemon(true);
            return thread;
        }
    });
    public static void senEmail(String host, String port, String account, String password, String from, String to, String subject, String msg) {
        emailThreadPool.submit(() -> {
            if (StringUtil.isBlank(account) || StringUtil.isBlank(password)) {
                sendEmailWithoutAuth(host, port, from, to, subject, msg);
            } else {
                sendEmailWithAuth(host, port, account, password, from, to, subject, msg);
            }
        });
    }

    private static void sendEmailWithAuth(String host, String port, String account,String password, String from, String to, String subject, String msg) {
        // 获取系统属性
        Properties properties = System.getProperties();

        // 设置邮件服务器
        properties.setProperty("mail.transport.protocol", "smtp");
        properties.setProperty("mail.smtp.host", host);
        properties.setProperty("mail.smtp.port", port);

        // 获取默认session对象
        Session session = Session.getDefaultInstance(properties);
        session.setDebug(true);
        try{
            // 创建默认的 MimeMessage 对象
            MimeMessage message = new MimeMessage(session);

            // Set From: 头部头字段
            message.setFrom(new InternetAddress(from));

            // Set To: 头部头字段
            message.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(to));

            // Set Subject: 头部头字段
            message.setSubject(subject);

            // 设置消息体
            message.setText(msg);

            // 发送消息
            Transport transport = session.getTransport();
            transport.connect(account, password);
            transport.sendMessage(message, message.getAllRecipients());
            System.out.println("Sent message successfully....");
        }catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }

    private static void sendEmailWithoutAuth(String host, String port, String from, String to, String subject, String msg) {
        // 获取系统属性
        Properties properties = System.getProperties();

        // 设置邮件服务器
        properties.setProperty("mail.smtp.host", host);
        properties.setProperty("mail.smtp.port", port);
        properties.setProperty("mail.transport.protocol", "smtp");
        // 获取默认session对象
        Session session = Session.getDefaultInstance(properties);
        session.setDebug(true);
        try{
            // 创建默认的 MimeMessage 对象
            MimeMessage message = new MimeMessage(session);

            // Set From: 头部头字段
            message.setFrom(new InternetAddress(from));

            // Set To: 头部头字段
            message.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(to));

            // Set Subject: 头部头字段
            message.setSubject(subject);

            // 设置消息体
            message.setText(msg);

            // 发送消息

            Transport.send(message, message.getAllRecipients());
            System.out.println("Sent message successfully....");
        }catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }
}
